﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quexcel.OpenXML.Interfaces;
using System.Xml;
using System.IO.Packaging;
using System.IO;

namespace Quexcel.OpenXML.Excel.SharedString.IO
{
    internal class SharedStringWriter : SharedStringReader, ISharedStringWriter
    {
        private XmlDocument sharedStringDoc;
        private PackagePart sheetPart;

        internal SharedStringWriter(PackagePart workbookPart, XmlNamespaceManager nsManager)
            : base(workbookPart, nsManager)
        {
            
        }

        public int WriteSharedString(string value)
        {
            return WriteSharedString(value, GetSize());
        }

        public int WriteSharedString(string value, int index)
        {
            return -1;
        }

        public bool Flush()
        {
            try
            {
                sharedStringDoc.Save(sheetPart.GetStream(FileMode.Create, FileAccess.Write));
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                this.Close();
            }
        }

        public void Close()
        {
            sharedStringDoc = null;
        }

        public bool Reset()
        {
            try
            {
                sharedStringDoc.Load(sheetPart.GetStream());
                return true;
            }
            catch
            {
                return false;
            }
        }

        #region IWriter Members

        public bool Write(string addressName, string value)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
